package com.atjava.nebula.eims.console.action;

import java.util.List;

import javax.persistence.EntityManagerFactory;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.jbpm.runtime.manager.impl.DefaultRuntimeEnvironment;
import org.kie.api.KieBase;
import org.kie.api.KieServices;
import org.kie.api.io.ResourceType;
import org.kie.api.runtime.EnvironmentName;
import org.kie.api.runtime.KieContainer;
import org.kie.api.runtime.KieSession;
import org.kie.api.runtime.manager.RuntimeEngine;
import org.kie.api.runtime.manager.RuntimeEnvironment;
import org.kie.api.runtime.manager.RuntimeEnvironmentBuilder;
import org.kie.api.runtime.manager.RuntimeManager;
import org.kie.api.runtime.manager.RuntimeManagerFactory;
import org.kie.api.task.TaskService;
import org.kie.api.task.model.TaskSummary;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.support.AbstractPlatformTransactionManager;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.atjava.nebula.core.exception.baseExp.BusinessException;
import com.atjava.nebula.core.exception.baseExp.SystemException;
import com.atjava.nebula.core.util.context.SpringContextHolder;
import com.atjava.nebula.web.action.BaseAction;
import com.atjava.nebula.web.action.BaseJbpmAction;

@Controller
@RequestMapping("/eims/console")
public class ConsoleAction extends BaseJbpmAction{
	
	@RequestMapping("/mytask/list")
	@ResponseBody
	public List<TaskSummary> myTaskList(
			HttpServletRequest request, 
			HttpServletResponse response) throws SystemException, BusinessException {
		
		UserDetails userDetails = (UserDetails) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		String username = userDetails.getUsername();
		
		/*RuntimeManager manager = (RuntimeManager) SpringContextHolder.getBean("runtimeManager");
        RuntimeEngine engine = manager.getRuntimeEngine(null);
        KieSession ksession = engine.getKieSession();
        TaskService taskService = engine.getTaskService();*/
		
		KieSession ksession = getKieSession();
		ksession.startProcess("leave-note");
		
		List<TaskSummary> tasks = taskService.getTasksOwned(username, "zh-CN");
		return tasks;
	}

}
